#!/usr/bin/evn python
#-*- coding:utf-8 -*-

def parse_xml_iter(data, root_str):
	root_len = len(root_str) + 2
	root_lene = root_len + 1
	records = []
	lp = sn = 0
	while 1:
		ps = data.find("<%s>" % root_str, lp)
		if ps < 0: break
		data_s = ps + root_len
		pe = data.find("</%s>" % root_str, data_s)
		if ps < 0: break
		record = data[data_s:pe]
		item = {}
		flp = 0
		sn += 1
		while 1:
			fs = record.find('<', flp)
			if fs < 0: break
			fss = fs + 1
			fe = record.find('>', fss)
			if fe < 0: break
			fname = record[fss:fe]
			if not fname: break
			fbs = fs + 2 + fe - fss
			fb = record.find("</%s>" % fname, fbs)
			if fb < 0: break
			fds = fb - fbs
			fdata = record[fbs:fb]
			if fds >= 12 and fdata.find('<![CDATA[') >= 0  and fdata.find(']]>') >= 0:
				fdata = fdata[9:-3]
			item[fname] = fdata
			flp = fb + 3 + fe - fss
		item['sn'] = sn
		if sn == 1: item['isfirst'] = 1
		records.append(item)
		lp = pe + root_lene
	return records
		
if __name__ == '__main__':
	from GFunc import geturl
	flag, t = geturl('http://192.168.2.8/entry/jianbao.php')
	m = parse_xml_iter(t, 'record')
	for i in m:
		print '************************'
		for k, v in i.items():
			print k
			print v
			print '---------------'